package cn.wolfcode.wolf2w.strategy.util;


import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import cn.wolfcode.wolf2w.strategy.ann.CurrentUser;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class UserInfoArgumentResolver  implements HandlerMethodArgumentResolver {

    //springMVC在读取每个参数时会调用此方法,验证当前参数是否符合要求,符合要求supportsParameter 返回 true 否则返回 false
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        boolean b = parameter.getParameterType() == Long.class && parameter.hasParameterAnnotation(CurrentUser.class);
        return b;
    }


    //当前面supportsParameter 返回true时,当前方法被调用,用于参数值的注入
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        Long userId = SecurityContextHolder.getUserId();
        return userId;
    }
}
